package jmine.tec.security.impl.web.pages.grupousuario;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.report.impl.table.ReportTableBuilder;
import jmine.tec.web.pages.HelpTextMessages;
import jmine.tec.web.wicket.component.command.img.DeleteCommand;
import jmine.tec.web.wicket.component.command.img.EditCommand;
import jmine.tec.web.wicket.component.command.img.ImageCommand;
import jmine.tec.web.wicket.pages.form.CrudPage;
import jmine.tec.web.wicket.pages.form.FormType;

import org.apache.wicket.Page;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.persist.dao.BaseDAOFactory;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;
import bancosys.tec.security.impl.dao.GroupDAO;
import bancosys.tec.security.impl.domain.Group;

/**
 * Tela para cadastro de {@link Group} de usuário.
 * 
 * @author diego.ferreira
 * @created Jan 7, 2011
 */
@Secure(id = "URL_LIST_GRUPO_USUARIO", permissionType = UrlPermission.class)
public class PesquisaGroup extends CrudPage<Group> {

    /**
     * Construtor
     */
    public PesquisaGroup() {
        super();
    }

    /**
     * Construtor
     * 
     * @param pageParameters pageParameters
     */
    public PesquisaGroup(PageParameters pageParameters) {
        super(pageParameters);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return HelpTextMessages.GROUP_PESQUISA;
    }

    /**
     * {@inheritDoc}
     */
    public List<Group> search(BaseDAOFactory daoFactory) {
        GroupDAO dao = daoFactory.getDAOByClass(GroupDAO.class);
        return dao.findByLikeNaturalKey(this.getModel().getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void addSearchFields(WebMarkupContainer container) {
        container.add(this.createTextField("name"));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void addResultTableColumns(ReportTableBuilder<Group> table) {
        table.addStringColumn("name", "Grupo", "name");
    }

    /**
     * {@inheritDoc}
     */
    public Page createNewPage() {
        return new CadastroGroup(this.createModel());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Page createFormPage(Group entity, FormType formType) {
        return new CadastroGroup(entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected List<ImageCommand> getTableCommands(Group entity) {
        List<ImageCommand> commands = new ArrayList<ImageCommand>();
        commands.add(new EditCommand(this));
        commands.add(new DeleteCommand(this));
        return commands;
    }
}
